/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets_controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import accesoDatos.*;
import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import logica.*;

/**
 *
 * @author Oswaldo
 */
public class ProgramasServlet extends HttpServlet {
    DaoPrograma daoPrograma = new DaoPrograma();

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ProgramasServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ProgramasServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String nombre;
        String codigo;
        String nivel;
        int creditos;
        
        String boton = request.getParameter("submit").toString();
        
        if (boton.equals("Guardar")){
            nombre = request.getParameter("nombre").toString();
            codigo = request.getParameter("codigo").toString();
            nivel  = request.getParameter("nivel").toString();
            creditos = Integer.parseInt(request.getParameter("creditos").toString());
            
            Programa p= new Programa();
            p.setCodigo(codigo);
            p.setNombre(nombre);
            p.setNivel(nivel);
            p.setCreditos(creditos);
            try{
            daoPrograma.guardarPrograma(p);
            //out.print("Programa Guardado");

             out.println("<html>");
            out.println("<head>");
            out.println("<title>Programa guardado</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h3>  El programa a sido guardado exitosamente"+ "</h3><br>");
            out.println("</body>");
            out.println("</html>");
            }
            catch(Exception e){
                out.print("Error al guarar");
            }
            
        }
        
        if (boton.equals("Consultar")){
            codigo = request.getParameter("codigo").toString();
            Programa p;
            try{
            p=daoPrograma.consultarPrograma(codigo);

            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Programa Consultado</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h3>  El codigo consultado es " + p.getCodigo() + "</h3><br>");
            out.println("<h3>  El nombre consultado es " + p.getNombre() + "</h3><br>");
            out.println("<h3>  El nivel consultado es " + p.getNivel() + "</h3><br>");
            out.println("<h3>  Los creditos son " + p.getCreditos() + "</h3><br>");
            
            out.println("</body>");
            out.println("</html>");
            
           // out.print("Programa Consultado ");

            //out.print("codigo: " + p.getCodigo());
            //out.print("Nombre: " + p.getNombre());
            //out.print("Nivel: " + p.getNivel());
            //out.print("Creditos: " + p.getCreditos());

            }
            catch(Exception e){
                out.println("<h3> NO HAY NADA PARA CONSULTAR"+ "</h3><br>");
            }
        }
        
        
        if (boton.equals("Modificar")){

            nombre = request.getParameter("nombre").toString();
            codigo = request.getParameter("codigo").toString();
            nivel = request.getParameter("nivel").toString();
            creditos = Integer.parseInt(request.getParameter("creditos").toString());
            try{

             Programa p = new Programa();

        p.setCodigo(codigo);
        p.setNombre(nombre);
        p.setNivel(nivel);
        p.setCreditos(creditos);

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Programa modificado</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h3>  El programa a sido modificado"+ "</h3><br>");

            out.println("</body>");
            out.println("</html>");
         //out.print("Va a Modificar");

          daoPrograma.actualizarPrograma(p);
            }
            catch(Exception e){
                out.print("Error al consultar");
            }

          }
        
        
        if (boton.equals("Eliminar")){

         codigo = request.getParameter("codigo").toString();
         daoPrograma.borrarPrograma(codigo);

          out.println("<html>");
            out.println("<head>");
            out.println("<title>Programa borrado</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h3>  El programa a sido borrado"+ "</h3><br>");

            out.println("</body>");
            out.println("</html>");



            //out.print("Va aeliminar");
        }
        
        /*
        if (isValidUser==true){
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Login Sucessfull</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Login Sucessfull " + request.getContextPath () + "</h1>");
            out.println("<p>Welcome</p> " + userName);
            out.println("</body>");
            out.println("</html>");

        }
        else{
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Login is not Sucessfull</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Login is not Sucessfull " + request.getContextPath () + "</h1>");
            out.println("<p>Wrong Username Or Password</p> ");
            out.println("You may want to <A HREF=\"LoginPage.html\">try again</A>");
            out.println("</body>");
            out.println("</html>");

        }
         * 
         */
        out.close();
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}//end class
